Imports System.Data.SqlClient
Imports Suprema
Imports DevComponents.DotNetBar
Public Class frmBiometria_Admin
    Private mstrCondicion As String
    Private mstrValor As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property

    Private Sub frmBiometria_Admin_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
       
    End Sub
    Private Sub frmBiometria_Admin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        timerStatus.Enabled = False
        'lblTitulo.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        'Line4.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)

        btnActualizar.ForeColor = Color.White
        btnActualizar.BackColor = Color.DodgerBlue

        lblCatalogo.ForeColor = Color.White
        lblCatalogo.BackColor = System.Drawing.Color.FromArgb(52, 57, 61)
        lblSubtitulo.ForeColor = Color.WhiteSmoke
        lblSubtitulo.BackColor = System.Drawing.Color.FromArgb(52, 57, 61)
        lblLastUpdateCatalogos.BackColor = System.Drawing.Color.FromArgb(52, 57, 61)
        lblLastUpdateCatalogos.ForeColor = System.Drawing.Color.FromArgb(155, 155, 155)
        Line7.ForeColor = System.Drawing.Color.FromArgb(85, 85, 85)
        Line8.ForeColor = System.Drawing.Color.FromArgb(85, 85, 85)
        Line9.ForeColor = System.Drawing.Color.FromArgb(85, 85, 85)


        btnActualizar.Enabled = False
        CargaUsuarios()

        Dim continua As Boolean
        continua = False



        If BIOMETRIA_INICIADA = False Then
            PASO_UNO(Me)
            continua = PASO_DOS()
        End If



        If continua = True Then
            lvDatabaseList.Columns.Add("Serial", 50, HorizontalAlignment.Left)
            lvDatabaseList.Columns.Add("Codigo", 60, HorizontalAlignment.Left)
            lvDatabaseList.Columns.Add("Nombre", 200, HorizontalAlignment.Left)

            lstPersonal.Columns.Add("Codigo", 60, HorizontalAlignment.Left)
            lstPersonal.Columns.Add("Nombre", 200, HorizontalAlignment.Left)

            txtBuscar.Clear()
            cbScanTemplateType.SelectedIndex = 0


            btnActualizar.Enabled = True


            Dim ListaAgregados As List(Of Object) = UpdateDatabaseList(Me)
            Dim listItem As ListViewItem

            tileConectar.TileColor = Metro.eMetroTileColor.Green


            For Each elemento In ListaAgregados
                listItem = lvDatabaseList.Items.Add(Convert.ToString(elemento(0)))
                listItem.SubItems.Add(elemento(1))
                listItem.SubItems.Add(elemento(2))
            Next
        Else

            tileConectar.TileColor = Metro.eMetroTileColor.Gray
            btnActualizar.Enabled = False
        End If
        
       

    End Sub

    Sub GenerarNotificacion(ByVal Texto As String, ByVal Duracion As Integer, ByVal colores As eToastGlowColor)
        Try
            Dim glow As eToastGlowColor = colores
            Dim Poe As eToastPosition
            If Me.WindowState = FormWindowState.Maximized Then
                Poe = eToastPosition.MiddleCenter
            Else
                Poe = eToastPosition.BottomCenter
            End If

            ToastNotification.Show(Me, Texto, Nothing, Duracion * 1000, glow, Poe)
        Catch ex As Exception
            If colores = eToastGlowColor.Red Then
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.OcurrioError)
            Else
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.MuestraStatus)
            End If
        End Try



    End Sub
    Sub CargaUsuarios()
        lstPersonal.Items.Clear()


        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        Windows.Forms.Cursor.Current = Cursors.WaitCursor


        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text.Trim)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        strSql = "SELECT CLAVE,NOMBRE_COMPLETO FROM EMPLEADOS WHERE ESTATUS<>'B' AND CLAVE NOT IN (SELECT CODIGO FROM Biometria) AND (upper(CLAVE) Like '" & Filtro & "' " & mstrCondicion & "  OR upper(Nombre_COMPLETO) like '" & Filtro & "' " & mstrCondicion & ")  Order By NOMBRE_COMPLETO"

        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            AgregaEmpleado(rdBuscar("CLAVE"), rdBuscar("NOMBRE_COMPLETO"))
        Loop
    End Sub
    Private Sub AgregaEmpleado(ByVal Codigo As String, ByVal Nombre As String)
        Dim listItem As ListViewItem
        listItem = lstPersonal.Items.Add(Codigo)
        listItem.SubItems.Add(Nombre)

    End Sub
 
    Private Sub cbScanTemplateType_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbScanTemplateType.SelectedIndexChanged
        If (m_Scanner Is Nothing) Then
            'tbxMessage.AppendText("Select data" & vbNewLine)
            Exit Sub
        End If


        Select Case cbScanTemplateType.SelectedIndex

            Case 0
                m_Scanner.nTemplateType = 2001
            Case 1
                m_Scanner.nTemplateType = 2002
            Case 2
                m_Scanner.nTemplateType = 2003

        End Select
    End Sub

    Private Sub btnTodosRegresar_Click(sender As System.Object, e As System.EventArgs)
        Terminar()
        Close()

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs)
        pbImageFrame.Image = Nothing
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs)
        PASO_UNO(Me)
        PASO_DOS()

    End Sub
    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaUsuarios()

    End Sub
   
    Private Sub btnEliminar_Click_1(sender As System.Object, e As System.EventArgs)

    End Sub

    
    Private Sub btnActualizar_Click(sender As System.Object, e As System.EventArgs) Handles btnActualizar.Click
        'ATENCION AQUI
        Dim IdEmpleado As String = BuscarIntDatoCitra("SELECT IDEMPLEADO FROM EMPLEADOS WHERE UPPER(NOMBRE_COMPLETO)='" & UCase(lstPersonal.SelectedItems(0).SubItems(1).Text) & "'")
        If IdEmpleado = 0 Then
            GenerarNotificacion("El empleado no existe o es baja", 5, eToastGlowColor.Red)
            Exit Sub
        End If

        Dim Codigo As String = BuscarStrDatoCitra("SELECT CLAVE FROM EMPLEADOS WHERE IDEMPLEADO=" & IdEmpleado)

        Dim IdUser As Integer = BuscarIntDatoBiometria("SELECT Serial FROM fingerprints WHERE UserID='" & Codigo & "'")
        If IdUser > 0 Then
            GenerarNotificacion("La persona indicada ya tiene un registro de huella dactilar", 5, eToastGlowColor.Red)
            Exit Sub
        End If

        If (Not ExtractTemplate(m_Template1, m_Template1Size, Me, pbImageFrame)) Then
            Exit Sub
        End If
        'Dim dlg As UserInfoForm = New UserInfoForm(False)
        Dim ufd_res As UFD_STATUS


        'dlg.ShowDialog(Me)
        'If (dlg.DialogResult <> Windows.Forms.DialogResult.OK) Then
        'tbxMessage.AppendText("User data input is cancelled by user" & vbNewLine)
        'Exit Sub
        'End If
        Dim Bio As New clsBiometria

        ufd_res = m_Database.AddData(Codigo, Convert.ToInt32(2), m_Template1, m_Template1Size, Nothing, 0, "MEMO")
        If (ufd_res <> UFD_STATUS.OK) Then
            UFDatabase.GetErrorString(ufd_res, m_strError)
            GenerarNotificacion("Ocurrio el error: " & m_strError, 5, eToastGlowColor.Red)
        Else
            Bio.Codigo = lstPersonal.SelectedItems(0).SubItems(0).Text
            Bio.Insertar()
            lstPersonal.Items.Remove(lstPersonal.SelectedItems(0))
            pbImageFrame.Image = Nothing
            cbScanTemplateType.Enabled = False
        End If

        lvDatabaseList.Items.Clear()

        Dim ListaAgregados As List(Of Object) = UpdateDatabaseList(Me)
        Dim listItem As ListViewItem

        For Each elemento In ListaAgregados
            listItem = lvDatabaseList.Items.Add(Convert.ToString(elemento(0)))
            listItem.SubItems.Add(elemento(1))
            listItem.SubItems.Add(elemento(2))
        Next
    End Sub

    Private Sub btnRegresarCatalogo_Click(sender As System.Object, e As System.EventArgs) Handles btnRegresarCatalogo.Click
        Close()
    End Sub

    Private Sub btnElimina_Click(sender As System.Object, e As System.EventArgs) Handles btnElimina.Click
        Dim ufd_res As UFD_STATUS
        Dim Serial As Integer

        If (lvDatabaseList.SelectedIndices.Count = 0) Then
            GenerarNotificacion("Seleccione el registro a eliminar", 5, eToastGlowColor.Red)
            'tbxMessage.AppendText("Selecciona un registro" & vbNewLine)
            Exit Sub
        Else
            Serial = Convert.ToInt32(lvDatabaseList.SelectedItems(0).SubItems(DATABASE_COL_SERIAL).Text)
            Call SetSqlCitra("DELETE FROM Biometria WHERE Codigo='" & lvDatabaseList.SelectedItems(0).SubItems(1).Text & "'")
            pbImageFrame.Image = Nothing
        End If

        ufd_res = m_Database.RemoveDataBySerial(Serial)
        If (ufd_res = UFD_STATUS.OK) Then
            GenerarNotificacion("Registro eliminado", 5, eToastGlowColor.Blue)
            UpdateDatabaseList(Me)
            CargaUsuarios()
        Else
            UFDatabase.GetErrorString(ufd_res, m_strError)
            GenerarNotificacion("Error al eliminar el registro: " & m_strError, 5, eToastGlowColor.Red)
        End If
    End Sub
End Class